// 文章相关 reducer

import { ArticleDetail } from '@/types/data'
import { ArticleAction } from '@/types/store'

// 文章类型定义
type ArticleState = {
  detail: ArticleDetail
}

// 文章reducer 初始化state
const initialState = {
  detail: {},
} as ArticleState

export const article = (
  state = initialState,
  action: ArticleAction
): ArticleState => {
  // 获取文章详情
  if (action.type === 'article/getArticleById') {
    return {
      ...state,
      detail: action.payload,
    }
  }
  // 统一处理 关注、收藏、点赞 三个状态
  if (action.type === 'article/updateInfo') {
    return {
      ...state,
      detail: {
        ...state.detail,
        [action.payload.name]: action.payload.value,
      },
    }
  }
  return state
}
